package com.example.mypplication.sortlist.newsortlist


import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import com.example.mypplication.R
import com.example.mypplication.databinding.ActivityContactListBinding
import com.example.mypplication.sortlist.CharacterParser
import com.example.mypplication.sortlist.SortModel
import java.util.*

class ContactListActivity : AppCompatActivity() {

    private lateinit var binding: ActivityContactListBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityContactListBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupToolbar()
        setupContactList()
        setupSearchView()
    }

    private fun setupToolbar() {
        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        binding.toolbar.setNavigationOnClickListener { finish() }
    }

    private fun filledData(date: Array<String>): List<com.example.mypplication.sortlist.newsortlist.SortModel> {
        return date.map {
            com.example.mypplication.sortlist.newsortlist.SortModel().apply {
                name = it
                sex = Random().nextInt(2)
                val pinyin = CharacterParser.getInstance().getSelling(it)
                val sortString = pinyin.substring(0, 1).uppercase(Locale.getDefault())
                sortLetters = if (sortString.matches("[A-Z]".toRegex())) sortString else "#"
            }
        }
    }

    private fun setupContactList() {
        // 显示加载状态
        binding.progressBar.visibility = View.VISIBLE

        // 模拟加载数据
//        val mockContacts = createMockContacts()
        val mockContacts = filledData(resources.getStringArray(R.array.date)) // 填充数据

        // 设置数据到自定义View
        binding.contactListView.setContacts(mockContacts)

        // 设置点击监听
        binding.contactListView.onItemClickListener = { contact ->
            Toast.makeText(this, "点击了: ${contact.name}", Toast.LENGTH_SHORT).show()
        }

        // 设置群发按钮监听
        binding.contactListView.onGroupSendClickListener = {
            Toast.makeText(this, "进入群发模式", Toast.LENGTH_SHORT).show()
        }

        // 隐藏加载状态
        binding.progressBar.visibility = View.GONE
    }

    private fun setupSearchView() {
//        binding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
//            override fun onQueryTextSubmit(query: String): Boolean {
//                filterContacts(query)
//                return true
//            }
//
//            override fun onQueryTextChange(newText: String): Boolean {
//                filterContacts(newText)
//                return true
//            }
//        })
    }

    private fun filterContacts(query: String) {
        // 实际项目中这里应该过滤数据
        Toast.makeText(this, "搜索: $query", Toast.LENGTH_SHORT).show()
    }

    private fun createMockContacts(): List<SortModel> {
        val names = arrayOf(
            "张三", "李四", "王五", "赵六", "钱七",
            "Alice", "Bob", "Charlie", "David", "Emma",
            "刘备", "关羽", "张飞", "诸葛亮", "曹操"
        )

        return names.map { name ->
            SortModel().apply {
                this.name = name
                sex = Random().nextInt(2) // 随机性别
                val pinyin = CharacterParser.getInstance().getSelling(name)
                val sortString = pinyin.substring(0, 1).uppercase(Locale.getDefault())
                sortLetters = if (sortString.matches("[A-Z]".toRegex())) sortString else "#"
            }
        }
    }
}